package cn.izeno.wx.models

import org.dom4j.Document
import org.dom4j.DocumentHelper

/**
 * <h1>微信公众号文本消息</h1>
 *
 * @author 陈治谋 (chenzhimou@tele-sing.com)
 * @version V1.0, 2014-12-21
 */
class TextMessage : Message(msgType = MSG_TYPE_TEXT) {

  var content: String = ""

  override fun parseXML(document: Document) {
    super.parseXML(document)
    this.content = document.selectSingleNode("/xml/Content")?.text ?: ""
  }

  override fun toXMLString(): String {
    val r = DocumentHelper.createDocument()
    val root = r.addElement("xml")
    root.addElement("FromUserName").addCDATA(fromUserName)
    root.addElement("ToUserName").addCDATA(toUserName)
    root.addElement("CreateTime").text = createTime.toString()
    root.addElement("MsgType").addCDATA(msgType)
    root.addElement("Content").addCDATA(content)
    return r.asXML()
  }
}
